<?php

namespace app\api\logic;
use app\common\basics\Logic;
use app\service\TechnWeixinService;
use app\service\UserWeixinService;
use think\Db;


/**
 * 工作台统计
 * Class StatLogic
 * @package app\admin\logic\index
 */
class JieriLogic extends Logic
{
    public static function send($mu,$mobile){
        $shijian = (time()*1000);
        $params = [
            'MchId'=>'1029208',
            'AppId'=>'10021687423500195',
            'Version'=>'1.1.0',
            'Type'=>'3',
            'PhoneNumberSet'=>$mobile,
            'TemplateId'=>$mu,
            'TemplateParamSet'=>[''],
            'TimeStamp'=>(string)$shijian,
            'SignType'=>'MD5',
            'Signature' => strtoupper(md5('AppId=10021687423500195&MchId=1029208&SignType=MD5&TemplateId=70031089&TimeStamp='.(time()*1000).'&Type=3&Version=1.1.0')),
        ];

// 计算数字签名
        $key = 'bc6fe8535aa8493eab6bb63af5cb4a25'; // 这里替换为实际的 appkey
        ksort($params); // 按照参数名排序
        $noSignKey = array("Signature", "ContextParamSet", "TemplateParamSet", "SessionContextSet", "PhoneNumberSet", "SessionContext", "PhoneList", "phoneSet");
        $signStr = '';
        foreach ($params as $k => $value) {
            if(!in_array($k, $noSignKey)) {
                $signStr .= "{$k}={$value}&";
            }
        }
        $signStr .= "key={$key}";
//        print_r($signStr);
        $signature = strtoupper(md5($signStr));
        $params['Signature'] = $signature;
        $url = 'https://apis.shlianlu.com/sms/trade/template/send';
        $data = json_encode($params);
//        echo $data;echo '<br>';
        $result = self::request_post($url,$data);
        return $result;
    }

    public static function  request_post($url = '', $param = '') {
        if (empty($url) || empty($param)) {
            return false;
        }
//
//        $postUrl = $url;
//        $curlPost = $param;
//        $ch = curl_init();//初始化curl
//        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
//        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
//        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
//        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
//        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
//        $data = curl_exec($ch);//运行curl
//        curl_close($ch);
//
//        return $data;
        $headers = array('Content-Type: application/json');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, TRUE);   //is it optional?
        curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;

    }


}